<?php
namespace app\console\controller;

use think\Url;
use think\Db;
use think\Loader;
use think\Request;
use think\Controller;
use app\client\model\Express as ExpressModel;
use app\console\model\Member as MemberModel;
use app\console\model\MemberIntegral as MemberIntegralModel;
use app\console\model\MemberMessage as MemberMessageModel;

class MemberIntegral extends Controller
{

    /*
     * 列表
     */
    public function index(Request $request, MemberIntegralModel $memberIntegral)
    {
        // 获取参数
        $inquire = $request->param();

        // 搜索条件
        $condition['id'] = ['neq', '0'];
        if (!empty($inquire['title'])) $condition['title|username'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['status'])) $condition['status'] = ['eq', $inquire['status']];

        // 读取积分记录
        $data = Db::view('member_integral', 'id,member_id,title,number,integral,status,create_time')
            ->view('member', 'username,name', 'member_integral.member_id = member.id')
            ->where($condition)
            ->order(['id' => 'desc'])
            ->paginate(10, false, ['query' => $inquire]);


        return $this->fetch('index', [
            'mate_title' => '积分记录列表',
            'data'       => $data
        ]);
    }

    /*
     * 积分记录删除
     */
    public function destroy($id)
    {

        if (MemberIntegralModel::destroy($id)) {
            win('操作成功', Url::build('MemberIntegral/index'));
        }

        fail('操作失败');

    }

    /*
     * 积分记录编辑
     */
    public function update($id)
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            // $validate = Loader::validate('MemberIntegral');
            // if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new MemberIntegralModel())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('MemberIntegral/index'));
            }

            fail('操作失败');

        }

        // 读取数据
        $item = Db::view('member_integral', 'id,member_id,title,number,integral,status,create_time,tracking_number,express_value,name,phone,area_info,address')
            ->view('member', 'username', 'member_integral.member_id = member.id')
            ->view('member_address', 'name', 'member_address.member_id = member.id')
            ->where(['id' => $id])
            ->find();

        $express_list = ExpressModel::all();


        return $this->fetch('create', [
            "mate_title" => '积分记录查看',
            'item'       => $item,
            'express_lists' => $express_list,

        ]);
    }

    /*
     * 排列顺序
     */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            MemberIntegralModel::where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('MemberIntegral/index'));
    }


    /**
     * 积分清除
     */
    public function eliminate()
    {
        $t = time();
        $m = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));  //当天开始时间
        $n = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t)); //当天结束时间
        $start_time = $m - (60 * 60 * 24 * 360);
        $end_time = $n - (60 * 60 * 24 * 360);

        $ini['integral_time'] = ['between time', [$start_time, $end_time]];

        $data = (new MemberModel)->where($ini)->paginate(10, false);


        return $this->fetch('', [
            "mate_title" => '积分即将到期的用户',
            'data'       => $data,
        ]);

    }


    public function message()
    {
        $t = time();
        $m = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));  //当天开始时间
        $n = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t)); //当天结束时间
        $start_time = $m - (60 * 60 * 24 * 360);
        $end_time = $n - (60 * 60 * 24 * 360);

        $ini['integral_time'] = ['between time', [$start_time, $end_time]];

        $members = (new MemberModel)->where($ini)->field('id')->select();

        $m_str = [];
        foreach ($members as $value) {
            $m_str[] = [
                'type'        => 1,
                'member_id'   => $value['id'],
                'title'       => '尊敬的客户您的积分即将过期请注意',
                'content'     => '尊敬的客户您的积分即将过期请注意',
                'create_time' => date('Y-m-d H:i:s',time()),
            ];
        }

        $operation = (new MemberMessageModel)->insertAll($m_str);

        if ($operation) {
            win('操作成功', Url::build('console/MemberIntegral/eliminate'));
        } else {
            fail('操作失败');
        }


    }


    public function purge()
    {
        $t = time();
        $m = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));  //当天开始时间
        $n = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t)); //当天结束时间
        $start_time = $m - (60 * 60 * 24 * 365);
        $end_time = $n - (60 * 60 * 24 * 365);

        $ini['integral_time'] = ['between time', [$start_time, $end_time]];

        $data = (new MemberModel)->where($ini)->paginate(10, false);


        return $this->fetch('', [
            "mate_title" => '积分即将到期的用户',
            'data'       => $data,
        ]);
    }


    public function clear()
    {
        $t = time();
        $m = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));  //当天开始时间
        $n = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t)); //当天结束时间
        $start_time = $m - (60 * 60 * 24 * 365);
        $end_time = $n - (60 * 60 * 24 * 365);

        $ini['integral_time'] = ['between time', [$start_time, $end_time]];

        $members = (new MemberModel)->where($ini)->field('id')->select();
        $m_str = "";
        foreach ($members as $value) {
            $m_str = $value['id'] . ",";
        }
        $a = rtrim($m_str,',');
        $operation = (new MemberModel())->where('id','in',$a)->update(['integral'=>0]);

        if ($operation) {
            Db::name('member')->where($ini)->update(['integral_time' => time()]);
            win('操作成功', Url::build('console/MemberIntegral/index'));
        } else {
            fail('操作失败');
        }

    }

}
